import unittest
from django.test import TestCase
from selenium import webdriver
from selenium.webdriver.common.by import By
import sys
sys.path.append(r'D:\NetWork\micro-member-ui-automation')


# 导入包

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
from Capture.verification.input_code import input_vs
from selenium.common import exceptions
from Base.base_page import BasePage
from data.Config import config
from ddt import ddt,unpack,file_data

#不重复打开浏览器 
chrome_options=Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver=r'C:\Users\Administrator\AppData\Local\Programs\Python\Python39\chromedriver.exe'
driver=webdriver.Chrome(chrome_driver,chrome_options=chrome_options)
#窗口最大化
# driver.maximize_window()

#browser = webdriver.Chrome()
#print(driver.title)

# driver = webdriver.Chrome()

xp = 'xpath'

# 登录
class Login:
    def denglu(self):
        
        # driver.get('https://wememberwebtest.xinyartech.com/user/login?redirect=%2Fcarousel')
        driver.get('https://wemember.x-shop.cn/prepaid/PrepaidSchemeList')
        # driver.quit()
        driver.implicitly_wait(10)
        driver.find_element(By.XPATH,'//*[@id="username"]').send_keys("17689310241")
        driver.find_element(By.XPATH,'//*[@id="password"]').send_keys("a17689310241.") #123456
        try:

            img = driver.find_element('xpath','//img[@style="margin-top: 2px;"]').get_attribute('src')
            code = input_vs(config.path,img) 
            driver.find_element('xpath','//input[@placeholder="请输入验证码"]').send_keys(code)
            driver.find_element('xpath','//button[@type="submit"]').click()

        except exceptions :

            driver.find_element('xpath','//img[@style="margin-top: 2px;').click()
            img = driver.find_element('xpath','//img[@style="margin-top: 2px;"]').get_attribute('src')
            code = input_vs(config.path,img)
            driver.find_element('xpath','//input[@placeholder="请输入验证码"]').send_keys(code)
            driver.find_element('xpath','//button[@type="submit"]').click()
        driver.implicitly_wait(10)

# 进入商户后台
class IntoMerchant:
    def into(self):
        # 进入系统管理
        driver.implicitly_wait(10)
        driver.find_element(By.XPATH,'//span[text()="系统管理"]/../..').click()

        # 进入商户管理
        driver.implicitly_wait(10)
        driver.find_element(By.XPATH,'//*[@id="app"]/section/aside/div/ul/li[5]/ul/li[4]').click()

        driver.implicitly_wait(10)
        driver.find_element(By.XPATH,'//*[@id="app"]/section/section/main/div[2]/div/div/div/div[1]/form/div/div[1]/div/div[2]/div/span/input').send_keys(Keys.CONTROL, '互动商数微会员测试')

        # 查询
        driver.implicitly_wait(10)
        driver.find_element(By.XPATH,'//*[@id="app"]/section/section/main/div[2]/div/div/div/div[1]/form/div/div[5]/span/button[1]').click()


        driver.implicitly_wait(10)
        time.sleep( 0.5 )
        driver.find_element(By.XPATH,'/html/body/div[1]/section/section/main/div[2]/div/div/div/div[3]/div[2]/div/div/div/div/div[2]/div[2]/div/table/tbody/tr/td/span/a[3]').click()

        time.sleep(2)
        handles = driver.window_handles  
        driver.switch_to.window(handles[-1])
intoMerchant = IntoMerchant()

    
# 菜单
class Caidan:
    def youhuiquan_gl(self):
        # 点击优惠券管理
        driver.implicitly_wait(10)
        driver.find_element(By.XPATH,'//*[@id="app"]/section/aside/div/ul/li[5]').click()
    def chuzhi_gl(self):
        # 点击优惠券管理
        driver.implicitly_wait(10)
        driver.find_element(By.XPATH,'//*[@id="app"]/section/aside/div/ul/li[6]').click()
        
caidan = Caidan()

# 优惠券管理内部
class YouhuiquanGL:
    def daijinquan(self,config):

        # 点击代金券
        driver.implicitly_wait(10)
        driver.find_element(By.XPATH,'//*[@id="app"]/section/aside/div/ul/li[5]/ul/li[1]').click()
        #
        # # 新增
        driver.implicitly_wait(10)
        driver.find_element(By.XPATH,'//*[@id="app"]/section/section/main/div[2]/div/div/div/div[1]/form/div/div[8]/span/button[3]').click()

        # # 优惠券名称
        driver.implicitly_wait(10)
        time.sleep( 0.5 )
        driver.find_element_by_xpath("//input[@placeholder='最多输入25个汉字']").send_keys(config.dj_mc)

        # 币种代码
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//input[@placeholder='输入币种代码']").send_keys(config.dj_code)
        
        
        #面额
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//label[text()='面额']/../../div/div/span/div/div/input[@role='spinbutton']").send_keys(config.dj_me)

        #发券数量
        time.sleep(  0.5 )
        element = driver.find_element_by_xpath("//input[@aria-valuemin='1']")
        #选中全部文字
        element.send_keys(Keys.CONTROL+'a')
        #退格
        element.send_keys(Keys.BACKSPACE)
        time.sleep(  0.5 )
        element.send_keys(config.dj_fqsl)


        #每人限领
        time.sleep(  0.5 )
        element = driver.find_element_by_xpath("//input[@aria-valuemin='0']")
        #选中全部文字
        element.send_keys(Keys.CONTROL+'a')
        #退格
        element.send_keys(Keys.BACKSPACE)
        time.sleep(  0.5 )
        element.send_keys(config.dj_mrxl)


        #需支付金额
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//label[text()='需支付金额']/../../div/div/span/div/div/input[@role='spinbutton']").send_keys(config.dj_zfje)


        #适用门店
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//span[text()='选 择']/..").click()
        time.sleep(  1 )
        driver.find_element_by_xpath("//span[text()='所有门店']/..").click()
        time.sleep(  1 )
        driver.find_element_by_xpath("//span[text()='确 定']/..").click()


        #使用规则
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//textarea[@maxlength='300']").send_keys(config.dj_sygz)
 
        #点击保存
        time.sleep(  1 )
        driver.find_element_by_xpath("//span[contains(text(),'保 存')]/..").click()
        
    def manjianquan(self,config):

        # 点击满减券
        driver.implicitly_wait(10)
        driver.find_element(By.XPATH,'//*[@id="app"]/section/aside/div/ul/li[5]/ul/li[2]').click()
        
        #
        # # 新增
        driver.implicitly_wait(10)
        driver.find_element_by_xpath("//*[@id=\"app\"]/section/section/main/div[2]/div/div/div/div[2]/button[3]").click()


        # # 优惠券名称
        driver.implicitly_wait(10)
        time.sleep( 0.5 )
        driver.find_element_by_xpath("//input[@placeholder='最多输入25个汉字']").send_keys(config.mj_mc)


        #面额
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//label[text()='面额']/../../div/div/span/div/div/input[@role='spinbutton']").send_keys(config.mj_me)
        

        #发券数量
        time.sleep(  0.5 )
        element = driver.find_element_by_xpath("//input[@aria-valuemin='1']")
        #选中全部文字
        element.send_keys(Keys.CONTROL+'a')
        #退格
        element.send_keys(Keys.BACKSPACE)
        time.sleep(  0.5 )
        element.send_keys(config.mj_fqsl)


        #每人限领
        time.sleep(  0.5 )
        element = driver.find_element_by_xpath("//input[@aria-valuemin='0']")
        #选中全部文字
        element.send_keys(Keys.CONTROL+'a')
        #退格
        element.send_keys(Keys.BACKSPACE)
        time.sleep(  0.5 )
        element.send_keys(config.mj_mrxl)


        #需支付金额
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//label[text()='需支付金额']/../../div/div/span/div/div/input[@role='spinbutton']").send_keys(config.mj_zfje)
        
        #适用门店
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//span[text()='选 择']/..").click()

        time.sleep(  1 )
        driver.find_element_by_xpath("//span[text()='所有门店']/..").click()
        time.sleep(  1 )
        driver.find_element_by_xpath("//span[text()='确 定']/..").click()


        #使用规则
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//label[text()='使用规则']/../../div/div/span/textarea").send_keys(config.mj_sygz)

        #点击保存
        time.sleep(  1 )
        driver.find_element_by_xpath("//span[text()='保 存']/..").click()   
        
    def duihuanquan(self,config):

        # 点击兑换券
        driver.implicitly_wait(10)
        driver.find_element(By.XPATH,'//*[@id="app"]/section/aside/div/ul/li[5]/ul/li[4]').click()
        
        #
        # # 新增
        driver.implicitly_wait(10)
        driver.find_element_by_xpath("//*[@id=\"app\"]/section/section/main/div[2]/div/div/div/div[2]/button[3]").click()


        # # 优惠券名称
        driver.implicitly_wait(10)
        time.sleep( 0.5 )
        driver.find_element_by_xpath("//input[@placeholder='最多输入25个汉字']").send_keys(config.dh_mc)


        #面额
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//label[text()='面额']/../../div/div/span/div/div/input[@role='spinbutton']").send_keys(config.dh_me)
        

        #发券数量
        time.sleep(  0.5 )
        element = driver.find_element_by_xpath("//input[@aria-valuemin='1']")
        #选中全部文字
        element.send_keys(Keys.CONTROL+'a')
        #退格
        element.send_keys(Keys.BACKSPACE)
        time.sleep(  0.5 )
        element.send_keys(config.dh_fqsl)


        #每人限领
        time.sleep(  0.5 )
        element = driver.find_element_by_xpath("//input[@aria-valuemin='-9007199254740991']")
        #选中全部文字
        element.send_keys(Keys.CONTROL+'a')
        #退格
        element.send_keys(Keys.BACKSPACE)
        time.sleep(  0.5 )
        element.send_keys(config.dh_mrxl)


        #需支付金额
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//label[text()='需支付金额']/../../div/div/span/div/div/input[@role='spinbutton']").send_keys(config.dh_zfje)
        
        #适用门店
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//span[text()='选 择']/..").click()

        time.sleep(  1 )
        driver.find_element_by_xpath("//span[text()='所有门店']/..").click()
        time.sleep(  1 )
        driver.find_element_by_xpath("//span[text()='确 定']/..").click()

        
        #适用商品
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//label[text()='适用商品']/../../div/div/span/textarea").send_keys(config.dh_sygz)
        
        #商品详情
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//label[text()='商品详情']/../../div/div/span/textarea").send_keys(config.dh_spxq)

        #使用规则
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//label[text()='使用规则']/../../div/div/span/textarea").send_keys(config.dh_sygz)

        #点击保存
        time.sleep(  1 )
        driver.find_element_by_xpath("//span[contains(text(),'保 存')]/..").click()   
        
    def zhouqiquan(self,config):

        # 点击周期券
        driver.implicitly_wait(10)
        driver.find_element(By.XPATH,'//*[@id="app"]/section/aside/div/ul/li[5]/ul/li[7]').click()
        
        # # 新增
        driver.implicitly_wait(10)
        driver.find_element_by_xpath("//*[@id=\"app\"]/section/section/main/div[2]/div/div/div/div[2]/button").click()


        # # 优惠券名称
        driver.implicitly_wait(10)
        time.sleep( 0.5 )
        driver.find_element_by_xpath("//input[@placeholder='请输入周期券名称']").send_keys(config.zqq_mc)


        #需兑换金额
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//input[@aria-valuemin='0']").send_keys(config.zqq_xdhje)
        

        #发券数量
        time.sleep(  0.5 )
        element = driver.find_element_by_xpath("//input[@aria-valuemin='1']")
        time.sleep(  0.5 )
        element.send_keys(config.zqq_ffzq)


        #发放数量
        time.sleep(  0.5 )
        element = driver.find_element_by_xpath("//input[@placeholder='请输入发放数量']")
        time.sleep(  0.5 )
        element.send_keys(config.zqq_ffsl)


        #每人限购
        time.sleep(  0.5 )
        driver.find_element_by_xpath('//label[text()="每人限购"]/../../div/div/span/div/div/input[@role="spinbutton"]').send_keys(config.zqq_mrxg)
        
        
        #优惠券
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//span[text()='选 择']/..").click()

        time.sleep(  1 )
        driver.find_element_by_xpath("(//a[text()='选择'])[1]").click()

        

        #使用规则
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//textarea[@placeholder='请输入使用规则']").send_keys(config.zqq_sygz)

        #点击提交
        time.sleep(  1 )
        driver.find_element_by_xpath("//span[text()='提 交']/..").click()   
        
    def quanbao(self,config):

        # 点击券包
        driver.implicitly_wait(10)
        driver.find_element(By.XPATH,'//*[@id="app"]/section/aside/div/ul/li[5]/ul/li[5]').click()
        
        # # 新增
        driver.implicitly_wait(10)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/div[2]/div/div/div/div[2]/button').click()


        # # 券包名称
        driver.implicitly_wait(10)
        time.sleep( 0.5 )
        driver.find_element_by_xpath("//input[@placeholder='最多输入25个汉字']").send_keys(config.qb_name)


        #需兑换金额
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//input[@aria-valuemin='0.01']").send_keys(config.qb_money)
        
        #发放数量
        time.sleep(  0.5 )
        element = driver.find_element_by_xpath("//input[@aria-valuenow='1']")
        #选中全部文字
        element.send_keys(Keys.CONTROL+'a')
        #退格
        element.send_keys(Keys.BACKSPACE)
        time.sleep(  0.5 )
        element.send_keys(config.qb_quantity)


        #每人限购
        time.sleep(  1 )
        driver.find_element_by_xpath("//label[text()='每人限购']/../../div/div/span/div/div/input[@role='spinbutton']").send_keys(config.qb_limit)
        

        #使用规则
        time.sleep(  0.5 )
        driver.find_element_by_xpath("//textarea[@placeholder='请输入使用规则']").send_keys(config.qb_rule)

        #点击保存
        time.sleep(  1 )
        driver.find_element_by_xpath("//span[text()='保 存']/..").click() 
        
        #优惠券管理
        driver.implicitly_wait(10)
        time.sleep(  3 )
        
        driver.find_element_by_xpath('(//div[@class="ant-table-body-outer"]/div/table/tbody/tr/td/span/a[text()="优惠券管理"])[1]').click()
#         driver.find_element_by_xpath('(//a[(text()="优惠券管理")])//../a[3]').click()
        
        #添加优惠券
        time.sleep(  1 )
        driver.find_element_by_xpath("//span[(text()='添加优惠券')]/..").click() 
        
        #点击选择
        time.sleep(  1 )
        driver.find_element_by_xpath("(//a[text()='选择'])[1]").click() 
        
        #添加优惠券
        time.sleep(  1 )
        driver.find_element_by_xpath("//span[(text()='添加优惠券')]/..").click() 
        
        #点击选择
        time.sleep(  1 )
        driver.find_element_by_xpath("(//a[text()='选择'])[2]").click() 
        
        #点击保存
        time.sleep(  1 )
        driver.find_element_by_xpath("(//span[(text()='保 存')]/..)[2]").click() 
    
    def youhuiquancelue(self,config):

        # 点击券包
        driver.implicitly_wait(10)
        driver.find_element(By.XPATH,'//*[@id="app"]/section/aside/div/ul/li[5]/ul/li[3]').click()
        
        # # 新增
        driver.implicitly_wait(10)
        driver.find_element_by_xpath('//*[@id="app"]/section/section/main/div[2]/div/div/div/div[2]/button').click()


        # # 策略名称
        driver.implicitly_wait(10)
        time.sleep( 0.5 )
        driver.find_element_by_xpath("//input[@placeholder='请输入优惠券组名']").send_keys(config.cl_name)


        # 限制条件（天数）
        time.sleep(  0.5 )
        s1 = driver.find_element_by_xpath("(//div[@class='ant-select-selection__rendered'])[3]").click()
        time.sleep(  1.5)
        driver.find_element_by_xpath('//div[@class="ant-select-dropdown-content"]/ul/li[2]').click()
        # 点击确定
        time.sleep( 1 )
        driver.find_element_by_xpath('//div[@class="ant-modal-footer"]/div/button[@class="ant-btn ant-btn-primary"]').click()
        
        # 点击优惠券列表
        time.sleep(  1 )
        driver.find_element_by_xpath('//div[@class="ant-table-body"]/table/tbody/tr/td/span/a[text()="优惠券列表"]').click()
        
        # 点击添加优惠券
        time.sleep(  1 )
        driver.find_element_by_xpath('//span[text()="添加优惠券"]/..').click()
        
        # 选择优惠券
        time.sleep(  1 )
        driver.find_element_by_xpath("(//label[@class='ant-checkbox-wrapper'])[2]").click()   
        time.sleep(  1 )
        driver.find_element_by_xpath("(//button[@class='ant-btn ant-btn-primary'])[8]").click()   
        time.sleep(  1 )
        driver.find_element_by_xpath("//span[text()='关 闭']/..").click() 
        
        
youhuiquanGL = YouhuiquanGL()



if __name__=="__main__":
    unittest.main()